use clap::Args;
use clap::CommandFactory;
use clap::Parser;

#[derive(Parser, Debug)]
#[command(version, about="clap practice", long_about = None)]
pub struct CmdArgs {
    #[arg(short, long)]
    db: String,

    #[arg(short, long)]
    ip: String,

    #[arg(long)]
    port: String,

    #[arg(short, long)]
    user: String,

    #[arg(long)]
    password: String,
}

#[command(name = "app")]
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct AppArgs {
    #[arg(short, long, required = false)]
    table: Option<String>,//Option必需和required = false搭配使用
}

#[cfg(test)]
mod tests {
    use crate::args::AppArgs;
    use crate::args::CmdArgs;
    use clap::Parser;
    #[test]
    fn main() {
        let args = CmdArgs::try_parse_from(vec![
            "test",
            "--db",
            "mysql",
            "--ip",
            "127.0.0.1",
            "--port",
            "3306",
            "--user",
            "root",
            "--password",
            "123456",
        ]);

        match args {
            Ok(a) => {
                println!("args:{:?}", a.db);
            }
            Err(err) => panic!("{:?}", err),
        }
    }

    #[test]
    fn test_app() {
        let args = AppArgs::try_parse_from(vec!["test"]);
        match args {
            Ok(a) => {
                println!("args:{:?}", a.table);
            }
            Err(err) => panic!("{:?}", err),
        }
    }
}
